package io.gitee.bluemiaomiao.dataanalyzebackend.exception;

import io.gitee.bluemiaomiao.dataanalyzebackend.api.CustomizeResultiml;
import io.gitee.bluemiaomiao.dataanalyzebackend.api.ResultCode;
import io.gitee.bluemiaomiao.dataanalyzebackend.util.ResultCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author ：TLang
 * @date ：Created in 2020/10/17 8:39
 * @description：全局异常处理
 * @modified By：
 * @version: 1.0$
 */
@Slf4j
@ControllerAdvice
public class GlobalException {

  /**
   * @author: TLang
   * @description 捕获自定义测试异常
   * @param e 测试异常
   * @return ResultCodeUtil
   * @create time: 2020/10/17 9:09
   */
  @ExceptionHandler(value = TestException.class)
  @ResponseBody
  public ResultCodeUtil test(Exception e) {
    //      logger.error("业务日志", e);
    TestException myException = (TestException) e;
    System.out.println(myException.getCode() + myException.getMessage());
    ResultCode resultCode =
        new CustomizeResultiml(Integer.parseInt(myException.getCode()), myException.getMessage());
    return ResultCodeUtil.failed(resultCode);
    //    logger.error("系统日志", e);
    //    ResultCode resultCode = new CustomizeResultiml(1, "异常错误");
    //    return ResultCodeUtil.failed(resultCode, "业务繁忙");
  }

  @ExceptionHandler(value = Exception.class)
  @ResponseBody
  public ResultCodeUtil exception(Exception e) {
    log.error("操作错误", e);
    return ResultCodeUtil.failed();
  }
}
